package com.cc.app.love;

import android.animation.TypeEvaluator;
import android.graphics.PointF;

public class BezierEvaluator implements TypeEvaluator<PointF> {

    private PointF p1, p2;

    public BezierEvaluator(PointF p1, PointF p2) {
        this.p1 = p1;
        this.p2 = p2;
    }

    public BezierEvaluator(PointF p) {
        this.p1 = p;
    }


    @Override
    public PointF evaluate(float t, PointF startValue, PointF endValue) {
        PointF point = new PointF();

        if (p2 != null) {
            point.x = (float) (startValue.x * Math.pow(1 - t, 3) + 3 * p1.x * t * Math.pow(1 - t, 2)
                    + 3 * p2.x * Math.pow(t, 2) * (1 - t)
                    + endValue.x * Math.pow(t, 3));

            point.y = (float) (startValue.y * Math.pow(1 - t, 3) + 3 * p1.y * t * Math.pow(1 - t, 2)
                    + 3 * p2.y * Math.pow(t, 2) * (1 - t)
                    + endValue.y * Math.pow(t, 3));
        } else {
            point.x = ((1 - t) * (1 - t) * startValue.x + 2 * t * (1 - t) * p1.x + t * t * endValue.x);
            point.y = ((1 - t) * (1 - t) * startValue.y + 2 * t * (1 - t) * p1.y + t * t * endValue.y);
        }

        return point;
    }
}
